/**
 * 
 */
package com.iwords.service.ao.impl;

import com.iwords.enums.KnownLevel;
import com.iwords.service.SysLibService;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;


import com.iwords.service.SysLibServiceLocal;
import com.iwords.service.object.SysLibEntity;
import com.iwords.service.persistence.LearningStatusPersistence;
import com.iwords.service.persistence.SysLibPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-13    
 * @description  系统课本-IMPL       
 */
@Stateless
public class SysLibServiceImpl implements SysLibServiceLocal, SysLibService {

    @EJB
    SysLibPersistence sysLibPersistence;
    @EJB
    LearningStatusPersistence learningStatusPersistence;

    public int count() {
        return sysLibPersistence.count();
    }

    public SysLibEntity findSysLibById(Long syslibid) {
        return sysLibPersistence.find(syslibid);
    }

    public List<SysLibEntity> findSysLibs() {
        return sysLibPersistence.findSysLibs();
    }

    public List<SysLibEntity> findRootSysLibs() {
        return sysLibPersistence.findRootSysLibs();
    }

    public long getStudyLibNum(Long userid, Long syslibid, int level) {
        return learningStatusPersistence.getStudyLibNum(userid, syslibid, level);
    }

    public List<SysLibEntity> getChildrenList(Long libid) {
        return sysLibPersistence.getChildrenList(libid);
    }

    public List<SysLibEntity> getAllStudyList(Long userid, int toPage) {
        List<Long> studyLibIdList = learningStatusPersistence.getAllStudyList(userid, toPage);

        List<SysLibEntity> list = new ArrayList<SysLibEntity>();

        for (Long id : studyLibIdList) {

            SysLibEntity en = sysLibPersistence.find(id);

            int all = en.getQuantity();

            long known = learningStatusPersistence.getStudyLibNum(userid, id, KnownLevel.Known.getValue());

            en.setPercent(known * 100 / all);
            list.add(en);

        }

        return list;
    }

    public SysLibEntity getLibByLibName(String libName) {

        return sysLibPersistence.getLibByLibName(libName);
    }

    public int getAllStudyListNum(Long userid) {
        return learningStatusPersistence.getAllStudyListNum(userid);
    }
}
